package com.stone.base.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.stone.common.util.RandomValidateCodeUtil;

/**
 * 验证码
 */
@Controller
@RequestMapping("/securityCode")
public class SecurityCodeController {
	@RequestMapping("/loadSecurityCode")
	public void loadSecurityCode(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "no-cache");//设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        
        try {
        	//输出图片方法
            String randomCode = RandomValidateCodeUtil.getRandcode(request, response);
            //将验证码放入session
            request.getSession().removeAttribute(RandomValidateCodeUtil.RANDOMCODEKEY);
            request.getSession().setAttribute(RandomValidateCodeUtil.RANDOMCODEKEY, randomCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
}
